﻿using System.Net;
using System.Net.Mail;
using System.Text;

namespace ShopB2C.Helper
{
    public class MailHelper
    {
        private int _port = 25;
        private string _hostName , _smtpUser , _smtpPassword;
        /// <summary>
        /// 邮件服务器端口
        /// </summary>
        public int Port
        {
            get { return _port; }
            set { _port = value; }
        }
        /// <summary>
        /// Smtp主机名
        /// </summary>
        public string HostName
        {
            get { return _hostName; }
            set { _hostName = value; }
        }
        /// <summary>
        /// Smtp用户名
        /// </summary>
        public string SmtpUser
        {
            get { return _smtpUser; }
            set { _smtpUser = value; }
        }
        /// <summary>
        /// Smtp密码
        /// </summary>
        public string StmpPassword
        {
            get { return _smtpPassword; }
            set { _smtpPassword = value; }
        }
        /// <summary>
        /// 默认空的构造函数
        /// </summary>
        public MailHelper()
        {
            // 默认空的构造函数
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="hostName">Smtp主机名</param>
        /// <param name="port">端口</param>
        public MailHelper(string hostName , int port)
        {
            this._hostName = hostName;
            this._port = port;

        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="hostName">Smtp主机名</param>
        /// <param name="port">端口</param>
        /// <param name="smtpUser">SMTP用户名</param>
        /// <param name="smtpPassword">SMTP密码</param>
        public MailHelper(string hostName , int port , string smtpUser , string smtpPassword)
        {
            this._hostName = hostName;
            this._port = port;
            this._smtpUser = smtpUser;
            this._smtpPassword = smtpPassword;
        }

        /// <summary>
        /// 使用异步发送邮件
        /// </summary>
        /// <param name="from">发件人</param>
        /// <param name="to">收件人</param>
        /// <param name="subject">主题</param>
        /// <param name="body">邮件正文</param>
        public void SendAsyncMail(string from , string to , string subject , string body)
        {

            SendMail(from , to , subject , body , true);
        }
        /// <summary>
        /// 使用同步发送邮件
        /// </summary>
        /// <param name="from">发件人</param>
        /// <param name="to">收件人</param>
        /// <param name="subject">邮件主题</param>
        /// <param name="body">邮件正文</param>
        public void SendMail(string from , string to , string subject , string body)
        {
            SendMail(from , to , subject , body , false);
        }
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="from">发件人</param>
        /// <param name="to">收件人</param>
        /// <param name="subject">邮件主题</param>
        /// <param name="body">邮件正文</param>
        /// <param name="isAsync">是否采用异步发送</param>
        private void SendMail(string from , string to , string subject , string body , bool isAsync)
        {
            // smtp类
            SmtpClient smtp = new SmtpClient(_hostName , _port);
            smtp.UseDefaultCredentials = true;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.Credentials = new NetworkCredential(_smtpUser , _smtpPassword);
            // message类
            MailMessage message = new MailMessage(from , to);

            message.Subject = subject;
            message.SubjectEncoding = Encoding.UTF8;

            message.Body = body;
            message.BodyEncoding = Encoding.UTF8;

            message.IsBodyHtml = true;
            // 异步发送邮件
            if (isAsync)
                smtp.SendAsync(message , null);
            else
                smtp.Send(message);
        }
    }
}
